namespace CSharpPro._1106;
using System.Collections.Generic;
using System.Text.RegularExpressions;

public class UserServiceImpl : IUserService
{
    private readonly List<User> _registeredUsers = new List<User>();

    public void Register()
    {
        Console.WriteLine("\n=== 用户注册 ===");

       
        string userName = GetValidatedUserName();

      
        string password = GetValidatedPassword();

       
        ValidateConfirmPassword(password);

       
        _registeredUsers.Add(new User(userName, password));
        Console.WriteLine("注册成功！即将返回主菜单...");
        PauseAndClear();
    }

    public bool Login(out string message)
    {
        Console.WriteLine("\n=== 用户登录 ===");

        
        string userName = GetInputWithEmptyCheck("请输入用户名：");

       
        string password = GetInputWithEmptyCheck("请输入密码：");

       
        foreach (var user in _registeredUsers)
        {
            if (user.UserName == userName)
            {
                if (user.Password == password)
                {
                    message = "登录成功！";
                    return true;
                }
                message = "密码错误！";
                return false;
            }
        }

        message = "用户名不存在！";
        return false;
    }

    public void ShowMainMenu()
    {
        Console.WriteLine("=== 主菜单 ===");
        Console.WriteLine("1. 登录");
        Console.WriteLine("2. 注册");
        Console.WriteLine("3. 退出");
        Console.Write("请选择功能（1-3）：");
    }

    public void Run()
    {
        while (true)
        {
             
            ShowMainMenu();
            string input = Console.ReadLine()?.Trim();

            if (!int.TryParse(input, out int choice))
            {
                Console.WriteLine("请输入有效数字！");
                PauseAndClear();
                continue;
            }

            switch (choice)
            {
                case 1:
                     HandleLogin();
                    break;
                case 2:
                    Register();
                    break;
                case 3:
                    Console.WriteLine("程序已退出，再见！");
                    return;
                default:
                    Console.WriteLine("请输入1-3之间的数字！");
                    PauseAndClear();
                    break;
            }
        }
    }

   
    private void HandleLogin()
    {
        bool success = Login(out string message);
        Console.WriteLine(message);
        
        if (success)
        {
            Console.WriteLine("正在进入系统首页...");
        }
        
        PauseAndClear();
    }

   
    private string GetValidatedUserName()
    {
        while (true)
        {
            string userName = GetInputWithEmptyCheck("请输入用户名：");

           
            if (userName.Length < 3 || userName.Length > 15)
            {
                Console.WriteLine("用户名长度必须为3-15个字符！");
                continue;
            }

          
            if (!Regex.IsMatch(userName, @"^[a-zA-Z0-9_]+$"))
            {
                Console.WriteLine("用户名只能包含字母、数字和下划线！");
                continue;
            }

           
            if (IsUserNameExists(userName))
            {
                Console.WriteLine("用户名已存在，请更换！");
                continue;
            }

            return userName;
        }
    }

   
    private string GetValidatedPassword()
    {
        while (true)
        {
            string password = GetInputWithEmptyCheck("请输入密码：");

           
            if (password.Length < 6 || password.Length > 20)
            {
                Console.WriteLine("密码长度必须为6-20个字符！");
                continue;
            }

          
            bool hasUpper = Regex.IsMatch(password, @"[A-Z]");
            bool hasLower = Regex.IsMatch(password, @"[a-z]");
            bool hasDigit = Regex.IsMatch(password, @"[0-9]");

            if (!hasUpper || !hasLower || !hasDigit)
            {
                Console.WriteLine("密码必须包含至少1个大写字母、1个小写字母和1个数字！");
                continue;
            }

            return password;
        }
    }

   
    private void ValidateConfirmPassword(string password)
    {
        while (true)
        {
            string confirmPwd = GetInputWithEmptyCheck("请输入确认密码：");
            
            if (confirmPwd == password)
                break;
                
            Console.WriteLine("密码与确认密码不一致，请重新输入！");
        }
    }

    
    private bool IsUserNameExists(string userName)
    {
        foreach (var user in _registeredUsers)
        {
            if (user.UserName == userName)
                return true;
        }
        return false;
    }

   
    private string GetInputWithEmptyCheck(string prompt)
    {
        while (true)
        {
            Console.Write(prompt);
            string input = Console.ReadLine()?.Trim();
            
            if (string.IsNullOrWhiteSpace(input))
            {
                Console.WriteLine("输入不可为空！");
                continue;
            }
            
            return input;
        }
    }

   
    private void PauseAndClear()
    {
        Console.WriteLine("按任意键继续...");
        Console.ReadKey();
        Console.Clear();
    }
}